import crmTypeModel from '@/pages/customerManagement/model/crmTypeModel.js';
export default {
    data() {
        return {
			isTotalCustomer: true,
            listData: [],
            totalNum: 0,
            limit: 15,
            page: 1,
            search: '',
            downStatus: 'nomore',
            rotateNum: 0,
            scrollTop: 0,
            type: 'all',
            crmType: 'customer'
        }
    },
    computed: {
        isHaveSeas() {
            return this.vuex_user.crm.pool && this.vuex_user.crm.pool.index ? this.vuex_user.crm.pool.index : false;
        },
        isSave() {
            return this.vuex_user.crm[this.crmType] && this.vuex_user.crm[this.crmType].save ? this.vuex_user.crm[this.crmType].save : false;
        }
    },

    onLoad(option) {
        this.type = option.type;
        this.crmType = option.crmType
        this.getPageList();
    },

    onShow() {
        this.setTitle();
        if (uni.getStorageSync("refreshData")) {
            this.scrollTop = 0;
            this.getPageList();
        }
    },

    methods: {
        setTitle() {
            let titles = {
                all: '全部客户',
                leads: '潜在客户阶段',
                target: '目标客户阶段',
                pool: '公海',
            };
            uni.setNavigationBarTitle({
				title: titles[this.type],
			});
        },
        //上拉刷新
        reachBottom() {
            // 此tab为空数据
            this.downStatus = 'loading';
            if (this.listData.length < this.totalNum) {
                this.getPageList('more');
            } else {
                this.downStatus = 'nomore';
            }
        },

        async getPageList(type) {
            let self = this;
            if (type == 'more') {
                self.page++;
            } else {
                self.page = 1;
            }
            let params = {
                page: self.page,
                limit: self.limit,
                type: crmTypeModel[this.crmType],
                search: self.search ? self.search.trim() : '',
            }
            if (this.type === 'all') {
                params.isTotalCustomer = true;
                params.isHaveSeas = this.isHaveSeas;
            }
            this.getCrmRequest(params).then(res => {
                uni.setStorageSync("refreshData", false);
                if (type == 'more') {
                    self.listData = self.listData.concat(res.data.list || []);
                } else {
                    self.listData = res.data.list || [];
                }
                self.downStatus = 'nomore';
                self.totalNum = res.data.totalRow || 0;
            }).catch(err => {
                uni.setStorageSync("refreshData", false);
                self.downStatus = 'nomore';
            })
        },

        cancelSearch() {
            this.search = '';
            this.scrollTop = 0;
            this.getPageList();
        },

        async getCrmRequest(data) {
            let apis = {
                'all': 'crmCustomerIndex',
                'leads': 'leadsList',
                'target': 'crmCustomerIndex',
                'pool': 'pollList',
                'followRecord': 'crmCustomerRecordIndex',
                'contacts': 'crmContactsIndex'
            };

			let res = await this.$api.customer.customer[apis[this.type]](data);;
			return res;
        },

        addData() {
            let params = {
                crmType: this.crmType
            }
            if (this.crmType == 'followRecord') {
                uni.navigateTo({
                    url: '/pages/customerManagement/followRecord/followRecordCreateView?params=' + encodeURIComponent(JSON.stringify(
                        params))
                })
            } else {
                uni.navigateTo({
                    url: '/pages/customerManagement/common/CrmCreateView?params=' + encodeURIComponent(JSON.stringify(
                        params))
                })
            }

        },

        refreshData() {
            this.rotateNum += 720;
            this.scrollTop = 0;
            this.getPageList();
        },

        changeScroll(e) {
            this.scrollTop = e.detail.scrollTop;
        },

        catchTouchMove() {
            return false
        }

    },

    // 页面处理函数--监听用户下拉动作
	async onPullDownRefresh() {
		await this.getPageList();
		uni.stopPullDownRefresh();
	},
	// 页面处理函数--监听用户上拉触底
	async onReachBottom() {
		await this.getPageList('more');
		uni.stopPullDownRefresh();
	},
}
